#coding=utf-8
#Boa:Dialog:DlgRecord

import wx
from ctrlAider import *
from product import CDataRecord

def create(parent):
    return DlgRecord(parent)

[wxID_DLGRECORD, wxID_DLGRECORDBTNDEL, wxID_DLGRECORDBTNMODIFY, 
 wxID_DLGRECORDBTNNEW, wxID_DLGRECORDLCRECORD, 
] = [wx.NewId() for _init_ctrls in range(5)]

class DlgRecord(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGRECORD, name=u'DlgRecord',
              parent=prnt, pos=wx.Point(214, 120), size=wx.Size(931, 650),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u4e8b\u9879\u7eaa\u5f55')
        self.SetClientSize(wx.Size(923, 616))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.btnNew = wx.Button(id=wxID_DLGRECORDBTNNEW,
              label=u'\u65b0\u5efa\u4e8b\u9879', name=u'btnNew', parent=self,
              pos=wx.Point(16, 8), size=wx.Size(93, 26), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGRECORDBTNNEW)

        self.btnDel = wx.Button(id=wxID_DLGRECORDBTNDEL, label=u'\u5220\u9664',
              name=u'btnDel', parent=self, pos=wx.Point(224, 8),
              size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGRECORDBTNDEL)

        self.btnModify = wx.Button(id=wxID_DLGRECORDBTNMODIFY,
              label=u'\u4fee\u6539', name=u'btnModify', parent=self,
              pos=wx.Point(120, 8), size=wx.Size(93, 26), style=0)
        self.btnModify.Show(True)
        self.btnModify.Enable(True)
        self.btnModify.Bind(wx.EVT_BUTTON, self.OnBtnModifyButton,
              id=wxID_DLGRECORDBTNMODIFY)

        self.lcRecord = wx.ListCtrl(id=wxID_DLGRECORDLCRECORD, name=u'lcRecord',
              parent=self, pos=wx.Point(16, 56), size=wx.Size(896, 544),
              style=wx.LC_REPORT)
        self.lcRecord.Bind(wx.EVT_LEFT_DCLICK, self.OnLcRecordLeftDclick)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()

                
    def myInit(self):
        cols = [(CDataRecord.fi_id, wx.LIST_FORMAT_LEFT, -1),
                (CDataRecord.fi_title, wx.LIST_FORMAT_LEFT, 200),
                (CDataRecord.fi_type, wx.LIST_FORMAT_LEFT, 100),
                (CDataRecord.fi_content, wx.LIST_FORMAT_LEFT, 200),
                (CDataRecord.fi_feedback, wx.LIST_FORMAT_LEFT, 200)]
        
        self.list = MyListCtrlDecorator(self.lcRecord, cols, CDataRecord)
        
    def OnBtnNewButton(self, event):
        import DlgRecordEditor
        
        dlg = DlgRecordEditor.create(self)
        if(dlg.ShowModal() == wx.ID_OK):
            self.list.refresh()

    def getSelection(self):
        item = self.list.getSeletion()
        if (item == -1):
            return None
        
        val = int(self.list.getItem(item, 0))
        return CDataRecord.getFirstInstance({CDataRecord.fi_id:val})

    def OnBtnDelButton(self, event):
        obj = self.getSelection()
        if (obj is None):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        obj.delete()
        self.list.refresh()

    def OnBtnModifyButton(self, event):
        import DlgRecordEditor
        
        obj = self.getSelection()
        if obj is None:
            return
        
        dlg = DlgRecordEditor.create(self)
        dlg.setData(obj)
        if(dlg.ShowModal() == wx.ID_OK):
            self.list.refresh()

    def OnLcRecordLeftDclick(self, event):
        import DlgRecordEditor
        
        obj = self.getSelection()
        if obj is None:
            return
        
        dlg = DlgRecordEditor.create(self)
        dlg.setData(obj, False)
        if(dlg.ShowModal() == wx.ID_OK):
            self.list.refresh()
